/*
 * Copyright (c) 2011 Joe Kopena <tjkopena@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*/

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.ui.Keyboard;

import com.rocketshipgames.haxe.Context;
import com.rocketshipgames.haxe.Engine;
import com.rocketshipgames.haxe.AssetCache;

import com.rocketshipgames.haxe.Entity;
import com.rocketshipgames.haxe.gfx.primitives.Spritesheet;

class Yoshi extends Entity {
  private static var spritesheet:Spritesheet = null;
  private var sprite:SpritesheetInstance;

  private static var IDLE:Int = 0;
  private static var WALK:Int = 1;

  public function new(x:Float, y:Float):Void
  {
    if (spritesheet == null) {
      spritesheet = new Spritesheet("assets/yoshi.png", 24, 32, true);
      spritesheet.setAnimation(IDLE, [2], false);
      spritesheet.setAnimation(WALK, [0, 1, 2, 3, 4]);
    }
    super(sprite = spritesheet.newInstance(), x, y);
    sprite.play(IDLE);
      // end function new
  }

  private static var SPEED:Float = 0.1;

  public override function update():Void
  {

    // These are done this way so they cancel out.
    var xvel:Float = 0;
    if (Context.keyDown(Keyboard.LEFT))
      xvel -= SPEED;
    if (Context.keyDown(Keyboard.RIGHT))
      xvel += SPEED;

    var yvel:Float = 0;
    if (Context.keyDown(Keyboard.UP))
      yvel -= SPEED;
    if (Context.keyDown(Keyboard.DOWN))
      yvel += SPEED;


    if (xvel == 0 && yvel == 0) {
      sprite.play(IDLE);
      //      sprite.stop();
    } else {
      if (xvel > 0)
	sprite.flipped = false;
      else if (xvel < 0)
	sprite.flipped = true;

      sprite.play(WALK);

      x += xvel * Context.elapsed;
      if (x > Context.screenWidth)
	x = 0;
      else if (x < -spritesheet.width)
	x = Context.screenWidth;

      y += yvel * Context.elapsed;
      if (y > Context.screenHeight)
	y = 0;
      else if (y < -spritesheet.height)
	y = Context.screenHeight;
    }

    // end function update
  }

  // end class Yoshi
}

class Ex15Keyboard extends Engine
{

  public function new():Void
  {
    trace("Starting");
    super(640, 480);

    add(new Yoshi(320, 240));
    // end new
  }

  private override function render():Void
  {
    Context.buffer.fillRect(Context.bufferRectangle,
			    0xffffff);
    super.render();
    // end function render
  }

   // Entry point for haXe
   static public function main()
   {
     // com.rocketshipgames.haxe.Debug.enableConsoleTrace();
     var cache:AssetCache = AssetCache.instance();
     cache.addBitmapData("assets/yoshi.png");
     cache.lock(function(){ new Ex15Keyboard(); });
     // end main
   }

  // end Ex15Keyboard
}
